package com.example.listenerproject;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MyPopupWindow extends PopupWindow implements View.OnClickListener {
    private Activity  mActivity;
    /** 根布局 */
    private View mRootView;
    private TextView mTvTakePicture;
    private TextView mTvTakeVideo;
    private TextView mTvCancel;
    private ChooseItemPopViewClickListener mChooseItemPopViewClickListener;


    public MyPopupWindow(Activity activity) {
        this.mActivity = activity;
        initConfig(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        initView();
    }

    public MyPopupWindow(Activity activity, int width, int height) {
        this.mActivity = activity;
        initConfig(width, height);
        initView();
    }

    private void initConfig(int width, int height) {
        this.setWidth(width);
        this.setHeight(height);
        this.setFocusable(true); //获取焦点
        this.setTouchable(true);//能够响应触摸事件
        this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置背景颜色
        this.setOutsideTouchable(true); // 获取外部触摸事件
        //关于touchable的设置
        //首先，我们要明确一点，点击到弹窗上的touch事件是首先传递到弹窗上的，如果你设置了touchable为true，那么当前弹窗则会消耗掉touch事件,当前的activity则不会再收到touch事件，反之则会收到事件。

        //关于focusable的设置
        //foucsable设置为true，弹窗在弹出后就获取到了焦点，焦点的获取，就告诉设备，外部按键事件向哪里传递。如果弹窗获取到了焦点，此时点击外部按键，按键事件是直接传递给弹窗的。需要注意的是设置focusable为true，并不代表这个控件就获取到了焦点，而是表示可以获取焦点。

        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//达到背景全部变暗的效果
//        mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
//                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//        this.setAnimationStyle(R.style.BottomPopuAnim);
        this.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
                mActivity.getWindow().setDimAmount(0f);
                params.alpha = 1.0f;
                mActivity.getWindow().setAttributes(params);
            }
        });
    }

    private void initView() {
        mRootView = LayoutInflater.from(mActivity).inflate(R.layout.mypopupwindow_layout, null, false);
        mTvCancel = mRootView.findViewById(R.id.tv_cancel);
        mTvTakePicture = mRootView.findViewById(R.id.tv_take_picture);
        mTvTakeVideo = mRootView.findViewById(R.id.tv_take_video);
        mTvCancel.setOnClickListener(this);
        mTvTakePicture.setOnClickListener(this);
        mTvTakeVideo.setOnClickListener(this);
        setContentView(mRootView);

    }

    /**
     * 显示
     * @param parent 关联父布局
     */
    public void show(View parent) {
        WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
        params.alpha = 0.7f; //透明度
        mActivity.getWindow().setDimAmount(0.7f);
        mActivity.getWindow().setAttributes(params);
        this.showAtLocation(parent, Gravity.BOTTOM, 0, 0); //设置popupwindow弹出的绝对位置-BOTTOM
        //设置弹出在某个控件的下方
        //this.showAsDropDown(xx)
    }


    @Override
    public void onClick(View v) {
        int vId = v.getId();
        if (vId==R.id.tv_take_picture){//点击按钮
            dismiss();  //弹框消失
            if(mChooseItemPopViewClickListener != null){
                mChooseItemPopViewClickListener.takePicture();
            }
        }else if (vId==R.id.tv_take_video){
            dismiss();
            if(mChooseItemPopViewClickListener != null){
                mChooseItemPopViewClickListener.takeVideo();
            }
        }else if (vId==R.id.tv_cancel){
            dismiss();
        }
    }

    public void setChooseItemPopViewClickListener(ChooseItemPopViewClickListener chooseItemPopViewClickListener) {
        this.mChooseItemPopViewClickListener = chooseItemPopViewClickListener;
    }

    public interface ChooseItemPopViewClickListener{
        public void takePicture();
        public void takeVideo();
    }
}
